package com.starblink.library.widget.sort

import android.content.Context
import android.graphics.drawable.RotateDrawable
import android.view.View
import com.google.android.material.button.MaterialButton
import com.starblink.android.basic.util.FontUtil
import com.starblink.basic.util.CommUtils
import razerdp.basepopup.BasePopupWindow

/**
 * 排序实用工具
 */
object SortUtils {
    private val priceSortBeans = mutableListOf(
        PriceSortBean(
            sortType = SortTypeEnum.DEFAULT,
            text = "Recommended",
        ),
        PriceSortBean(
            sortType = SortTypeEnum.PRICE_H2L,
            text = "Price: high to low",
        ),
        PriceSortBean(
            sortType = SortTypeEnum.PRICE_L2H,
            text = "Price: low to high",
        )
    )

    /**
     * 显示价格排序弹窗
     */
    fun showPriceSortPopup(
        context: Context,
        selectedSortType: SortTypeEnum? = null, //默认选中的排序类型
        alignView: View, // 弹窗显示依赖View
        sortView: MaterialButton, // 排序按钮
        onSelect: ((priceSortBean: PriceSortBean) -> Unit)? = null, //选中回调
    ) {
        // 显示排序弹窗
        val sortPop =
            PriceSortPop(context, selectedSortType = selectedSortType, onSelect = onSelect)
        sortPop.linkTo(alignView)
        sortPop.showPopupWindow(alignView)
        sortPop.setOnPopupWindowShowListener {
            (sortView.icon as RotateDrawable?)?.let {
                it.level = 10000 / 2
            }
        }
        sortPop.onDismissListener = object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                (sortView.icon as RotateDrawable?)?.let {
                    it.level = 0
                }
            }
        }
    }

    /**
     * 设置品牌排序按钮选中或未选中
     */
    fun setSortViewSelected(
        view: MaterialButton,
        isSelected: Boolean,
    ) {
        if (isSelected) {
            view.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.gu_text_primary))
            view.typeface =
                FontUtil.getTypeface(FontUtil.MONTSERRAT_SEMIBOLD)
        } else {
            // 仅未选中，设置默认图标
            view.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.gu_text_secondary))
            view.typeface =
                FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)
            view.icon =
                CommUtils.getDrawable(com.starblink.library.widget.R.drawable.ic_sort_discount_normal1)
        }
    }

}